import DList from "@/components/dlist";
import searchBox from "./components/search_box.vue";
export default {
  data() {
    return {
      activeName: "",
      order_by: 'new',
      type: "1",
      myLocation: {},
      classifyData: [],
      dataList: [],
      loading: false,
      finished: false,
      page: 1,
      name: ""
    };
  },
  activated() {
    this.init();
    this.type = this.$route.params.type + '';
    this.classify_id = this.$route.params.classify_id ? this.$route.params.classify_id*1 : '';
    this.myLocation = JSON.parse(localStorage.getItem("myLocation"));
    this.fun.setWXTitle(this.type == '1' ? '发布者需求列表' : this.type == '2' ? '技能需求列表' : '企业需求列表');
    this.getClassifyData();
  },
  methods: {
    async getData() {
      if (this.loading === true || this.finished === true) {
        return;
      }
      this.loading = true;
      let url = this.type == "1" ? 'plugin.be-within-call.frontend.public-search.need-order' : this.type == "2" ? 'plugin.be-within-call.frontend.public-search.shifu' : 'plugin.be-within-call.frontend.public-search.enterprise-need'
      let form = {};
      if( this.type == "1" ||  this.type == "2") {
        form ={
          lng: this.myLocation ? this.myLocation.point.lng : "",
          lat: this.myLocation ? this.myLocation.point.lat : "",
          name: "",
          page_size: 15,
          category_id: this.activeName,
          order_by: this.order_by,
          page: this.page
        }
      }else {
        form = {
          name: this.name,
          page_size: 15,
          category_id: this.activeName,
          page: this.page
        }
      }
      let { data, result, msg } = await $http.get(url, { ...form },'loading');
      if (result === 0) {
        this.$toast(msg);
        return;
      }
      if (data.data.length === 0 || data.data.length < data.per_page || data.current_page === data.last_page) {
        this.finished = true;
      }
      this.total = data.total;
      this.dataList.push(...data.data);
      this.page++;
      this.loading = false;
    },
    onLoad() {
      this.getData();
    },
    // 获取分类
    getClassifyData() {
      $http.get("plugin.be-within-call.frontend.index.get-category-list", { sort_type: this.type }).then(({ data , result , msg }) => {
        if (result) {
          this.classifyData = data;
          if(this.type == '3') {
            this.classifyData.unshift({
              id: "",
              title: "全部分类",
            })
          }
          this.activeName = this.$route.params.id !== '0' ? this.$route.params.id * 1 : data[0].id;
          if(this.classify_id) {
            this.classifyData = this.classifyData.filter(item => item.id == this.classify_id);
          }
          this.getData();
        } else {
          this.$toast(res.msg);
        }
      });
    },
    clickTypeOp(val) {
      if(this.loading)  { return }
      this.order_by = val;
      this.init();
      this.getData();
    },
    changeTab() {
      this.init();
      this.getData();
    },
    init() {
      this.loading = false;
      this.finished = false;
      this.dataList = [];
      this.page = 1;
    },
    // 跳转技师师傅主页
    goTechnicianHome(id) {
      this.$router.push(
        this.fun.getUrl("anytimeTechnicianHome", { id })
      );
    },
    // 跳转个人需求
    goividualNeeds(type,item) {
      console.log(item.order_id,'item.order_id');
      this.$router.push(
        this.fun.getUrl("anytimeIndividualNeeds", {type , order_id: item.id})
      );
    },
    inputValue() {
      this.init();
      this.getData();
    }
  },
  components: { DList , searchBox}
};
